package mymyoux.io
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mymyoux.events.LoadEvent;
	import mymyoux.events.MEventDispatcher;
	/**
	 * <b>Deprecated</b>
	 */
	public class LoadTextFile extends MEventDispatcher
	{
		static public const ERROR:String="error";
		static public const LOADING:String="loading";
		static public const OPEN:String="open";
		static public const COMPLETED:String="completed";
		protected var f:URLRequest;
		protected var u:URLLoader;
		protected var _args:Parameters;
		public function LoadTextFile(url:String,args:Parameters=null){
			f=new URLRequest();
			f.url=url;
			f.method="POST";
			u=new URLLoader();
			_args=args;
			if(args!=null){
				var v:URLVariables=new URLVariables();
				while(args.hasNext()){
					v[args.key]=args.value;
					args.next();
				}
				u.data=v;
			}
			u.addEventListener(IOErrorEvent.IO_ERROR,IOError);
			u.addEventListener(SecurityErrorEvent.SECURITY_ERROR,SecurityError);
			u.addEventListener(Event.COMPLETE,Completed);
			u.addEventListener(Event.OPEN,Open);
			u.addEventListener(ProgressEvent.PROGRESS,Progress);
			u.load(f);
			
		}
		protected function IOError(e:IOErrorEvent):void{
			this.dispatchEvent(new LoadEvent(f.url,_args,ERROR));
			destroy();
		}
		protected function SecurityError(e:SecurityErrorEvent):void{
			this.dispatchEvent(new LoadEvent(f.url,_args,ERROR));
			destroy();
		}
		protected function Completed(e:Event):void{
			try{
				this.dispatchEvent(new LoadEvent(f.url,_args,COMPLETED,(e.target as URLLoader).data,(e.target as URLLoader).bytesTotal,(e.target as URLLoader).bytesTotal));
			}catch(err:Error){
				this.dispatchEvent(new LoadEvent(f.url,_args,ERROR));
			}
			destroy();
		}
		protected function Open(e:Event):void{
			this.dispatchEvent(new LoadEvent(f.url,_args,OPEN));
		}
		protected function Progress(e:ProgressEvent):void{
			this.dispatchEvent(new LoadEvent(f.url,_args,LOADING,null,e.bytesLoaded,e.bytesTotal));
		}
		public override function destroy():void{
			u.removeEventListener(IOErrorEvent.IO_ERROR,IOError);
			u.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,SecurityError);
			u.removeEventListener(Event.COMPLETE,Completed);
			u.removeEventListener(Event.OPEN,Open);
			u.removeEventListener(ProgressEvent.PROGRESS,Progress);
			super.destroy();
		}
	}
}